<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>对接信息</span>
      </div>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            :disabled="!canChange"
            v-hasPermi="['eqxm:teamReception:add']"
            >新增</el-button
          >
        </el-col>
      </el-row>

      <el-table
        border
        v-loading="loading"
        :data="teamReceptionList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="接待单位" align="center" prop="receptionname" />
        <el-table-column label="接待人姓名" align="center" prop="receptionman" />
        <el-table-column label="职位" align="center" prop="position" />
        <el-table-column label="联系方式" align="center" prop="tel" />
        <el-table-column label="是否为校友" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <span v-if="scope.row.isalumni == 0">是</span>
            <span v-if="scope.row.isalumni == 1">否</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              :disabled="!canChange"
              v-hasPermi="['eqxm:teamReception:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <el-card v-if="showCard" class="box-card" style="margin-top: 20px">
      <div slot="header" class="clearfix">
        <span>已掌握基本情况</span>
      </div>
      <!-- <ckeditor :editor-url="editorUrl"   v-model="form.purpose" :config="editorConfig"></ckeditor> -->
      <ckeditor v-model="teamForm.basicInformation" :config="editorConfig0"></ckeditor>
    </el-card>
    <el-card v-if="showCard" class="box-card" style="margin-top: 20px">
      <div slot="header" class="clearfix">
        <span>活动期间安全措施</span>
      </div>
      <!-- <ckeditor :editor-url="editorUrl"   v-model="form.purpose" :config="editorConfig"></ckeditor> -->
      <ckeditor v-model="teamForm.safeMeasures" :config="editorConfig1"></ckeditor>
    </el-card>
    <!-- <el-button
      style="margin-top: 20px; float: right; margin-left: 10px"
      v-if="canChange"
      type="primary"
      @click="teamExamine"
      v-hasPermi="['eqxm:teamTeacher:add']"
      >审核进度</el-button
    > -->
    <el-button
      style="margin-top: 20px; float: right"
      v-if="canChange"
      type="primary"
      @click="submitFormTeam"
      v-hasPermi="['eqxm:teamTeacher:add']"
      >提交</el-button
    >
    <el-button
      v-hasPermi="['eqxm:team:edit']"
      style="float: right; margin-right: 10px; margin-top: 20px"
      type="primary"
      @click="back"
      >上一步</el-button
    >

    <!-- 添加或修改对接信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="接待单位" prop="receptionname">
          <el-input v-model="form.receptionname" placeholder="请输入接待单位" />
        </el-form-item>
        <el-form-item label="接待人姓名" prop="receptionman">
          <el-input v-model="form.receptionman" placeholder="请输入接待人姓名" />
        </el-form-item>
        <el-form-item label="职位" prop="position">
          <el-input v-model="form.position" placeholder="请输入职位" />
        </el-form-item>
        <el-form-item label="联系方式" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="是否校友" prop="isalumni">
          <el-select v-model="form.isalumni" placeholder="请选择">
            <el-option label="是" value="0"></el-option>
            <el-option label="否" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 审核进度对话框 -->
    <el-dialog :title="title" :visible.sync="teamExamineOpen" width="1000px" append-to-body>
      <div v-if="teamForm.status == 0">请填写完整后提交！</div>
      <div v-if="teamForm.status == 3">指导教师暂未审核！</div>
      <el-card v-if="teamForm.commentsOne" class="box-card">
        <div slot="header" class="clearfix">
          <span>指导教师意见</span>
        </div>
        <div>{{teamForm.commentsOne}}</div>
      </el-card>
      <el-card v-if="teamForm.commentsTwo" class="box-card" style="margin-top: 10px">
        <div slot="header" class="clearfix">
          <span>指导单位意见</span>
        </div>
        <div>{{teamForm.commentsTwo}}</div>
      </el-card>
      <el-card v-if="teamForm.commentsThree" class="box-card" style="margin-top: 10px">
        <div slot="header" class="clearfix">
          <span>学校团委意见</span>
        </div>
        <div>{{teamForm.commentsThree}}</div>
      </el-card>
      <!-- <div v-html="imgStyle(teamForm.commentsThree)"></div>
      <div v-html="imgStyle(teamForm.commentsTwo)"></div> -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="teamExamineOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTeamReception,
  getTeamReception,
  delTeamReception,
  addTeamReception,
  updateTeamReception,
  exportTeamReception,
} from '@/api/eqxm/teamReception';
import { listTeam, getTeam, delTeam, addTeam, updateTeam, exportTeam } from '@/api/eqxm/team';
import { teamStatusMap, teamStatusOptions } from '@/views/eqxm/enum';

import CommonUtils from '@/utils/CommonUtils';
import CKEditor from 'ckeditor4-vue';
import { mapGetters } from 'vuex';
export default {
  name: 'TeamReception',
  components: {
    ckeditor: CKEditor.component,
  },
  computed: {
    ...mapGetters(['roles', 'name']),
  },
  // filters: {
  //   teamStatusMap: function (value) {
  //     return teamStatusMap.get(value);
  //   },
  // },
  props: ['isAdmin'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 对接信息表格数据
      teamReceptionList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        receptionname: null,
        receptionman: null,
        position: null,
        tel: null,
        isalumni: null,
      },
      // 表单参数
      form: {
        receptionname: null,
        receptionman: null,
        position: null,
        tel: null,
        isalumni: null,
      },
      // 表单校验
      rules: {
        receptionname: [{ required: true, message: '接待单位不能为空', trigger: 'blur' }],
        receptionman: [{ required: true, message: '接待人不能为空', trigger: 'blur' }],
        position: [{ required: true, message: '职位不能为空', trigger: 'blur' }],
        tel: [
          { required: true, message: '联系方式不能为空', trigger: 'blur' },
          // {
          //   pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
          //   message: '请输入正确的手机号码',
          //   trigger: 'blur',
          // },
        ],
        isalumni: [{ required: true, message: '是否校友不能为空', trigger: 'blur' }],
      },
      isimportantOptions: [],
      presentationOptions: [],
      researchtopicOptions: [],
      teamId: '',
      editorConfig0: {
        readOnly: true,
      },
      editorConfig1: {
        readOnly: true,
      },
      teamForm: {},
      showCard: false,
      canChange: false,

      // 审核进度弹框
      teamExamineOpen: false,
      // 团队教师人数
      teacherTotal: null,
    };
  },
  created() {
    this.init();
    //this.getList();
  },
  methods: {
    init() {
      const id = CommonUtils.getCurTeamInfo().id;
      this.teamId = id;
      if (id != '') {
        getTeam(id).then(response => {
          console.log(response.data.createBy == this.name, '3eeee');
          this.showCard = true;
          this.teamForm = response.data;
          if(this.teamForm){
            this.teamForm.commentsOne = this.teamForm.commentsOne?this.teamForm.commentsOne.replace("<p>",""):""
            this.teamForm.commentsOne = this.teamForm.commentsOne?this.teamForm.commentsOne.replace("</p>",""):""
            this.teamForm.commentsTwo = this.teamForm.commentsTwo?this.teamForm.commentsTwo.replace("<p>",""):""
            this.teamForm.commentsTwo = this.teamForm.commentsTwo?this.teamForm.commentsTwo.replace("</p>",""):""
            this.teamForm.commentsThree = this.teamForm.commentsThree?this.teamForm.commentsThree.replace("<p>",""):""
            this.teamForm.commentsThree = this.teamForm.commentsThree?this.teamForm.commentsThree.replace("</p>",""):""
          }
          this.teamForm.researchtopic = this.teamForm.researchtopic + '';
          this.teacherTotal = this.teamForm.eqxmTeamTeacherList.length;
          this.form.teamid = this.teamId;
          var status =
            this.teamForm.status == 0 || this.teamForm.status == 2 || this.teamForm.status == 3;
          this.canChange = this.isAdmin || (response.data.createBy == this.name && status);
          if (this.canChange) {
            this.editorConfig0.readOnly = false;
            this.editorConfig1.readOnly = false;
          }
        });
        this.loading = true;
        this.queryParams.teamid = this.teamId;
        listTeamReception(this.queryParams).then(response => {
          this.teamReceptionList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      }
    },
    /** 查询对接信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.teamid = this.teamId;
      listTeamReception(this.queryParams).then(response => {
        this.teamReceptionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        receptionname: null,
        receptionman: null,
        position: null,
        tel: null,
        isalumni: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加对接信息';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getTeamReception(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '修改对接信息';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.teamid = this.teamId;
          if (this.form.id != null) {
            updateTeamReception(this.form).then(response => {
              if (response.code === 200) {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
              }
            });
          } else {
            addTeamReception(this.form).then(response => {
              if (response.code === 200) {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除对接信息编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return delTeamReception(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有对接信息数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return exportTeamReception(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        });
    },
    /** 提交按钮 */
    submitFormTeam() {
      console.log(this.teacherTotal, 'this.teacherTotal');
      if (this.teacherTotal <= 0) {
        this.$modal.msgError('团队指导老师不能为空！');
      } else {
        this.teamForm.teamid = this.teamId;
        this.$confirm('确定提交？', '提示', { type: 'warning' }).then(() => {
          updateTeam(this.teamForm).then(response => {
            // this.$modal.msgSuccess("修改成功");
            // this.$parent.jump(5);
            this.$router.push({
              //核心语句
              path: '/eqxm/team', //跳转的路径
            });
            this.commitExamine();
          });
        });
      }
    },

    commitExamine() {
      let comForm = {};
      comForm.id = this.teamId;
      if(!this.isAdmin || this.teamForm.status == 2 || this.teamForm.status == 0){
        comForm.status = '3'; //3
        updateTeam(comForm).then(response => {
          if (response.code === 200) {
          this.$modal.msgSuccess('修改成功');
          this.open = false;
          this.getList();
          }
        });
      }
    },
    back() {
      this.$parent.jump(3);
    },

    // 查看审核进度
    teamExamine() {
      this.teamExamineOpen = true;
      this.title = '审核进度：' + teamStatusMap.get(this.teamForm.status);
    },
    imgStyle(val) {
      if (val != '' && val != undefined && val != null) {
        console.log(
          val?.replace(
            /<img/g,
            '<img style="max-width: 100%!important;display: block;margin: 0 auto;"'
          ),
          '888'
        );
        return val?.replace(
          /<img/g,
          '<img style="max-width: 100%!important;display: block;margin: 0 auto;"'
        );
      } else {
        console.log(val, '88899');
        return val;
      }
    },
  },
};
</script>
